package com.idol.logger.core.printer.window.adapt

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.idol.logger.core.printer.data.LoggerMo

internal class IdolXAdapter(
    private val inflater: LayoutInflater
) : RecyclerView.Adapter<IdolXAdapter.Holder>() {

    private inner class Proxy : IdolAdaptProxy(inflater) {
        override fun notifyChangedItemInserted(position: Int) {
            notifyItemInserted(position)
        }
    }

    class Holder(
        root: View,
        val holder: IdolAdaptProxy.Holder
    ) : RecyclerView.ViewHolder(root)

    private val proxy = Proxy()

    fun add(item: LoggerMo) = also { proxy.add(item) }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
        val compat = proxy.onCreateViewHolder(parent)
        return Holder(compat.root, compat)
    }

    override fun getItemCount(): Int = proxy.count()

    override fun onBindViewHolder(holder: Holder, position: Int) {
        val compat = holder.holder
        proxy.onBindViewHolder(compat, position)
    }

}